﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using ProjectWizardManagement.Entity.Model;
using ProjectWizardManagementv1.ViewModels.Concrete;
using ProjectWizardManagementv1.WpfComponents.ComponentClasses;

namespace ProjectWizardManagementv1.Views
{
    /// <summary>
    /// Interaction logic for Workspace.xaml
    /// </summary>
    public partial class Workspace : UserControl
    {
        private List<ProjectFileViewModel> _selectedFilesList;
        private ProjectFileViewModel _focusedProjectFile;
        private TasksControl _tasksControl;
        private CalendarControl _calendarControl;

        public Workspace()
        {
            InitializeComponent();
            _tasksControl = new TasksControl();
            _calendarControl = new CalendarControl();
            Messenger.Default.Register<ClosableTab>(this, AddTab);
            _selectedFilesList = new List<ProjectFileViewModel>();
        }

        private void TreeViewItem_OnRightButtonDown(object sender, MouseEventArgs e)
        {
            var item = sender as TreeViewItem;
            if(item != null && !item.IsFocused)
            {
                item.Focus();
                e.Handled = true;
            }
        }

        private void AddTab(ClosableTab tab)
        {
            Tabs.Items.Add(tab);
            tab.Focus();
        }

        private void TasksControlView_Open(object sender, RoutedEventArgs e)
        {
            var tab = new ClosableTab {Content = _tasksControl};
            AddTab(tab);
        }

        private void CalendarControlView_Open(object sender, RoutedEventArgs e)
        {
            var tab = new ClosableTab {ClosableContent = _calendarControl};
            AddTab(tab);
        }

        private void ProjectFilesView_SelectedItemChanged(object sender, RoutedEventArgs routedEventArgs)
        {
            if(!IsControlKeyDown())
                RemoveSelectedList();

            _focusedProjectFile = ProjectFilesView.SelectedItem as ProjectFileViewModel;
            AddItemToSelectionList(_focusedProjectFile);
            Messenger.Default.Send(_selectedFilesList);
        }

        private void AddItemToSelectionList(ProjectFileViewModel item)
        {
            if(item != null)
            {
                if(!_selectedFilesList.Contains(item))
                    _selectedFilesList.Add(item);

                item.IsMultiSelected = true;
            }
        }

        private void RemoveSelectedList()
        {
            _selectedFilesList.ForEach(p => p.IsMultiSelected = false);
            _selectedFilesList.RemoveAll(p => true);   
        }

        private bool IsControlKeyDown()
        {
            return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void ProjectExplorer_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
        {
            e.Handled = true;
        }
    }
}